package com.feemanage.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.common.annotation.Anonymous;
import com.common.core.domain.AjaxResult;
import com.common.core.domain.R;
import com.common.core.page.PageDomain;
import com.common.core.page.TableDataInfo;
import com.common.utils.StringUtils;
import com.common.utils.poi.ExcelUtil;
import com.common.validate.add;
import com.common.validate.edit;
import com.feemanage.domain.BizBudgetAlteration;
import com.feemanage.domain.bo.FundsFeeShowBo;
import com.feemanage.domain.vo.BizBudegtAlterationVo;
import com.feemanage.domain.vo.BudegtAlterationVO;
import com.feemanage.service.IBizBudgetAlterationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 经费调账
 */
@Api(tags="经费调账")
@RestController
@RequestMapping("/feemanage/alteration")
public class BizBudgetAlterationControlller {


    @Autowired
    private IBizBudgetAlterationService baseService;

    /**
     * 查询列表
     * @param pageDomain
     * @param alteration
     * @return
     */
    @ApiOperation("查询列表")
    @GetMapping("/list")
    public TableDataInfo list(PageDomain pageDomain, BizBudgetAlteration alteration){
        IPage<BizBudegtAlterationVo> page = baseService.selectList(pageDomain,alteration);
        return TableDataInfo.build(page.getRecords(),page.getTotal());
    }

    /**
     * 检索表头统计
     * @param alteration
     * @return
     */
    @ApiOperation("检索表头统计")
    @GetMapping(value = "/listtotal")
    public AjaxResult listtotal(BizBudgetAlteration alteration)
    {
        List<HashMap<String, Integer>> list =baseService.selectListTotal(alteration);
        return AjaxResult.success(list);
    }

    /**
     * 检查项目
     * @param projectId
     * @return
     */
    @ApiOperation("检查项目是否可以调账，返回提示信息")
    @GetMapping("/checkproject")
    public String  checkProject(@NotEmpty(message = "未选择项目！") String projectId)
    {
        String checkProjectResult = baseService.checkProject(projectId);
        return checkProjectResult;
    }


    /**
     * 获取页面初始数据
     * @param showBo
     * @return
     */
    @ApiOperation("获取页面初始数据，新增时必传projectId,projectClass")
    @GetMapping("/getBudgetInfo")
    public R<BudegtAlterationVO> getBudgetInfo(FundsFeeShowBo showBo) {
        return R.ok(baseService.getBudgetInfo(showBo));
    }

    /**
     * 获取页面数据
     * @param showBo
     * @return
     */
    @ApiOperation("获取页面数据，编辑时传当前主表id--字段id")
    @GetMapping("/geteditInfo")
    public R<BudegtAlterationVO> geteditInfo(FundsFeeShowBo showBo) {
        return R.ok(baseService.geteditInfo(showBo));
    }

    /**
     * 导出经费调账列表
     */
    @ApiOperation("导出经费调账列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response,@RequestBody PageDomain pageDomain, @RequestBody BizBudgetAlteration alteration)
    {
        List <BizBudegtAlterationVo> list = baseService.selectExportList(pageDomain,alteration);
        ExcelUtil<BizBudegtAlterationVo> util = new ExcelUtil<>(BizBudegtAlterationVo.class);
        util.exportExcel(response, list, "经费调账列表");
    }


    /**
     * 新增
     * @param alterationVO
     * @return
     */
    @ApiOperation("新建")
    @PostMapping("/add")
    public R<Void> addBudgetAlteration(@Validated(add.class) @RequestBody BudegtAlterationVO alterationVO){
        String msg = baseService.addBudgetAlteration(alterationVO);
        return StringUtils.isEmpty(msg) ? R.ok() : R.fail(msg);
    }

    /**
     * 修改
     * @param alterationVO
     * @return
     */
    @ApiOperation("编辑")
    @PostMapping("/edit")
    public R<Void> editBudgetAlteration(@Validated(edit.class) @RequestBody  BudegtAlterationVO alterationVO){
        String msg = baseService.editBudgetAlteration(alterationVO);
        return StringUtils.isEmpty(msg) ? R.ok() : R.fail(msg);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @ApiOperation("删除")
    @DeleteMapping("/remove/{ids}")
    public R<Void> remove( @NotEmpty(message = "主键不能为空") @PathVariable String[] ids)
    {
        boolean result = baseService.delBudgetAlteration(Arrays.asList(ids),true);
        return result ? R.ok() : R.fail();
    }
}
